package com.aiti.myproject.client.widgets;

import java.util.List;

import com.aiti.myproject.EditImage;
import com.aiti.myproject.client.Project_II___Web;
import com.aiti.myproject.client.events.GalleryUpdatedEvent;
import com.aiti.myproject.client.events.GalleryUpdatedEventHandler;
import com.aiti.myproject.client.service.UserImageService;
import com.aiti.myproject.client.service.UserImageServiceAsync;
import com.aiti.myproject.shared.UploadImage;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PhotoGallery extends Composite implements
		GalleryUpdatedEventHandler {

	private static PhotoGalleryUiBinder uiBinder = GWT
			.create(PhotoGalleryUiBinder.class);

	@UiField
	FlexTable galleryTable;
	Project_II___Web main;
	private static final int GALLERY_WIDTH = 5;
	private UserImageServiceAsync userImageServiceAsync;

	interface PhotoGalleryUiBinder extends UiBinder<Widget, PhotoGallery> {
	}

	public PhotoGallery(Project_II___Web main) {
		this.main = main;
		initWidget(uiBinder.createAndBindUi(this));
		userImageServiceAsync = GWT.create(UserImageService.class);
		refreshGallery();
	}

	private void refreshGallery() {
		userImageServiceAsync
				.getRecentlyUploaded(new AsyncCallback<List<UploadImage>>() {

					@Override
					public void onSuccess(List<UploadImage> result) {
						// Window.alert("Update success");
						int curCol = 0;
						int curRow = 0;
						for (final UploadImage image : result) {
							Image widgetImage = createImageWidget(image);
							galleryTable.setWidget(curRow, curCol, widgetImage);
							curCol++;
							if (curCol >= GALLERY_WIDTH) {
								curRow++;
								curCol = 0;
							}
						}

					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error" + caught.toString());
					}
				});
	}

	private Image createImageWidget(final UploadImage image) {
		final Image widgetImage = new Image();
		widgetImage.setUrl(image.getServingUrl() + "=s250");
		final DecoratedPopupPanel popupPanel = new DecoratedPopupPanel(true);
		widgetImage.addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(MouseOverEvent event) {
				Widget widget = (Widget) event.getSource();
				int left = widget.getAbsoluteLeft() + 10;
				int top = widget.getAbsoluteTop() + widget.getOffsetHeight()
						+ 10;
				popupPanel.setWidth("150px");
				popupPanel.setWidget(new HTML("Upload: " + image.getCreateAt()));
				popupPanel.show();
				popupPanel.setPopupPosition(left, top);
			}
		});
		widgetImage.addMouseOutHandler(new MouseOutHandler() {

			@Override
			public void onMouseOut(MouseOutEvent event) {
				popupPanel.hide();
			}
		});

		widgetImage.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				ImageOverlay imageOverlay = new ImageOverlay(image, main
						.getLoginInfo());
				imageOverlay.addGalleryUpdatedEventHandler(PhotoGallery.this);

				/*final PopupPanel imagePopup = new PopupPanel(true);
				imagePopup.setAnimationEnabled(true);
				imagePopup.setWidget(imageOverlay);
				imagePopup.setGlassEnabled(true);
				imagePopup.setAutoHideEnabled(true);

				imagePopup.center();*/
				
				VerticalPanel verticalPanel = new VerticalPanel();
				verticalPanel.setBorderWidth(5);
				verticalPanel.setSize("800", "600");
				verticalPanel.add(imageOverlay);
				RootPanel.get("editImage").add(verticalPanel);
			}
		});
		return widgetImage;
	}

	@Override
	public void onGalleryUpdate(GalleryUpdatedEvent event) {
		refreshGallery();
	}
}
